import request from '@/utils/request';
import { getBaseURL } from '@/utils/config.js';

// 获取用户订单列表
export function getUserOrders(userId) {
    return request({
        url: `/order/user/${userId}`,
        method: 'get'
    });
}

// 获取订单详情
export function getOrderDetail(orderId) {
    return request({
        url: `/order/${orderId}`,
        method: 'get'
    });
}

// 创建订单
export function createOrder(orderData) {
    return request({
        url: '/order/create',
        method: 'post',
        data: orderData
    });
}

// 更新订单状态
export function updateOrder(orderData) {
    return request({
        url: '/order',
        method: 'put',
        data: orderData
    });
}

// 支付订单
export function payOrder(orderId) {
    const payUrl = `${getBaseURL()}/api/alipay/pay`;
    const token = '089d7338b893d016'; // 测试用token，可替换为实际登录token
    return fetch(payUrl, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'token': token
        },
        body: `orderId=${orderId}`
    })
        .then(response => response.text());
}

// 获取支付状态
export function getPaymentStatus(paymentId) {
    return request({
        url: `/payment/status/${paymentId}`,
        method: 'get'
    });
}

// 获取订单支付记录
export function getOrderPayments(orderId) {
    return request({
        url: `/payment/${orderId}`,
        method: 'get'
    });
}

// 退款
export function refundPayment(paymentId) {
    return request({
        url: `/payment/refund/${paymentId}`,
        method: 'post'
    });
}

// 确认收货
export function confirmReceive(orderId) {
    return request({
        url: '/order/confirm-receive',
        method: 'post',
        data: { orderId }
    });
}

// 取消订单
export function cancelOrder(orderId) {
    return request({
        url: '/order/cancel',
        method: 'post',
        data: { orderId }
    });
}

// 获取订单统计信息
export function getOrderStats(userId) {
    return request({
        url: '/order/stats',
        method: 'get',
        params: { userId }
    });
}

// 删除订单
export function deleteOrder(orderId) {
    return request({
        url: `/order/${orderId}`,
        method: 'delete'
    });
} 